﻿using System;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

// Token: 0x02000036 RID: 54
[RequireComponent(typeof(InputField))]
public class HexColorField : MonoBehaviour
{
	// Token: 0x06000127 RID: 295 RVA: 0x0000D1AC File Offset: 0x0000B3AC
	private void Awake()
	{
		this.hexInputField = base.GetComponent<InputField>();
		this.hexInputField.onEndEdit.AddListener(new UnityAction<string>(this.UpdateColor));
		this.hsvpicker.onValueChanged.AddListener(new UnityAction<Color>(this.UpdateHex));
	}

	// Token: 0x06000128 RID: 296 RVA: 0x0000D1FD File Offset: 0x0000B3FD
	private void OnDestroy()
	{
		this.hexInputField.onValueChanged.RemoveListener(new UnityAction<string>(this.UpdateColor));
		this.hsvpicker.onValueChanged.RemoveListener(new UnityAction<Color>(this.UpdateHex));
	}

	// Token: 0x06000129 RID: 297 RVA: 0x0000D237 File Offset: 0x0000B437
	private void UpdateHex(Color newColor)
	{
		this.hexInputField.text = this.ColorToHex(newColor);
	}

	// Token: 0x0600012A RID: 298 RVA: 0x0000D250 File Offset: 0x0000B450
	private void UpdateColor(string newHex)
	{
		if (!newHex.StartsWith("#"))
		{
			newHex = "#" + newHex;
		}
		Color currentColor;
		if (ColorUtility.TryParseHtmlString(newHex, out currentColor))
		{
			this.hsvpicker.CurrentColor = currentColor;
		}
		else
		{
			Debug.Log("hex value is in the wrong format, valid formats are: #RGB, #RGBA, #RRGGBB and #RRGGBBAA (# is optional)");
		}
	}

	// Token: 0x0600012B RID: 299 RVA: 0x0000D2A4 File Offset: 0x0000B4A4
	private string ColorToHex(Color32 color)
	{
		return (!this.displayAlpha) ? string.Format("#{0:X2}{1:X2}{2:X2}", color.r, color.g, color.b) : string.Format("#{0:X2}{1:X2}{2:X2}{3:X2}", new object[]
		{
			color.r,
			color.g,
			color.b,
			color.a
		});
	}

	// Token: 0x04000220 RID: 544
	public ColorPicker hsvpicker;

	// Token: 0x04000221 RID: 545
	public bool displayAlpha;

	// Token: 0x04000222 RID: 546
	private InputField hexInputField;
}
